package cn.aixuegao.web.admin;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.aixuegao.common.base.controller.BaseController;
import cn.aixuegao.common.constant.Const;
import cn.aixuegao.common.result.Response;
import cn.aixuegao.common.result.ResultMsg;
import cn.aixuegao.model.admin.Article;
import cn.aixuegao.service.admin.ArticleService;
import cn.aixuegao.utils.DateUtils;

@Controller
@RequestMapping(value="/admin")
public class ArticleController extends BaseController{
	
	@Autowired
	ArticleService articleService;
	/**
	 * 
	 * @Title:ArticleController
	 * @Description:获取某一篇文章的详情
	 * @param articleId
	 * @param model
	 * @return
	 */
	@GetMapping(value="/article/details/{article_id}")
	public String getDetail(@PathVariable(value="article_id") Integer articleId,Model model) {
		Article art = new Article();
		art.setId(articleId);
		Article article = articleService.selectById(art);
		model.addAttribute("article", article);
		return "admin/article_details";
	}
	/**
	 * 根据id获取blog文章列表
	 * @Title:ArticleController
	 * @Description:
	 * @param columnId
	 * @param model
	 * @return
	 */
	@GetMapping(value="/blog/{column_id}")
	public String getArticleList(@PathVariable(value="column_id") Integer columnId,Model model) {
		Article article = new Article();
		article.setColumnId(columnId);
		EntityWrapper<Article> ew = new EntityWrapper<>(article);
		ew.setSqlSelect("id,title,LEFT(content,150) AS content,publish_time,key_word,author,type");
		List<Article> articleList = articleService.selectList(ew);
		model.addAttribute("articleList", articleList);
		model.addAttribute("columnId", columnId);
		return "admin/blog";
	}
	
	/**
	 * 
	 * @Title:ArticleController
	 * @Description:
	 * @param articleId
	 * @param model
	 * @return
	 */
	@GetMapping(value="/article/edit/{article_id}")
	public String getArticleEdit(@PathVariable(value="article_id") Integer articleId,Model model) {
		Article art = new Article();
		art.setId(articleId);
		Article article = articleService.selectById(art);
		model.addAttribute("article", article);
		return "admin/article_edit";
	}
	/**
	 * 
	 * @Title:ArticleController
	 * @Description:跳转到新增页面
	 * @return
	 */
	@GetMapping(value="/article/form/{columnId}")
	public String getArticleForm(@PathVariable(value="columnId") String columnId, Model model) {
		model.addAttribute("columnId", columnId);
		return "admin/article_form";
	}
	/**
	 * 新增文章
	 * @Title:ArticleController
	 * @Description:
	 * @return
	 */
	@PostMapping(value="/article/add")
	@ResponseBody
	public Response getArticleAdd(Article article) {
		article.setType(Const.YES);
		Timestamp now = DateUtils.getNowTimestamp();
        article.setPublishTime(now);
        article.setModificationTime(now);
        article.setSourceId(UUID.randomUUID().toString().replace("-", ""));
		Boolean flag = articleService.insert(article);
		if(flag) {
			return resultMsg(ResultMsg.SUCCESS);
		}else {
			return resultMsg(ResultMsg.FAILED);
		}
	}
	/**
	 * 新增文章
	 * @Title:ArticleController
	 * @Description:
	 * @return
	 */
	@PostMapping(value="/article/edit")
	@ResponseBody
	public Response getArticleEdit(Article article) {
		article.setType(Const.YES);
		Timestamp now = DateUtils.getNowTimestamp();
        article.setModificationTime(now);
		Boolean flag = articleService.updateById(article);
		if(flag) {
			return resultMsg(ResultMsg.SUCCESS);
		}else {
			return resultMsg(ResultMsg.FAILED);
		}
	}
}
